package com.bawei.base.http

import androidx.lifecycle.LiveData

import androidx.lifecycle.MutableLiveData

class EventBus {
    companion object{
        val instance:EventBus by lazy {EventBus()}
    }
    //定义一个haspmap用来存在我们的事件<ivedata>
    val liveDataMap = HashMap<String,MutableLiveData<Any>>()
    //存入职  发送事件  泛型的
    fun <T> sendEvent(key:String,value:T){
        //当不存在时创建新的，  存在的进行重新赋值
        //
        if (!liveDataMap.containsKey(key)){
            liveDataMap[key] = MutableLiveData()
        }
        //存在的进行重新赋值
        (liveDataMap[key] as MutableLiveData<T>).value = value
    }

    //接收事件，并进行处理
    fun <T> getEvent(key: String):LiveData<T>{
        //不存在是创建新的
        if (!liveDataMap.containsKey(key)){
            liveDataMap[key] = MutableLiveData()
        }
        //当存在时获取他的值
        return liveDataMap[key] as LiveData<T>
    }
}